<include file="./Template/Admin/header.html" title="修改管理员" />
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>修改管理员</h5>
                </div>
                <div class="ibox-content">
                    <div class="row m-b-sm m-t-sm">
                        <div class="col-md-2">
                            <a href="{:U('Admin/admin/index')}" class="btn btn-outline btn-primary">返回管理员列表</a>
                        </div>
                    </div>
                    <form class="form-horizontal m-t" id="editForm" enctype="multipart/form-data" action="{:U('Admin/admin/Save')}">
                        <input type="hidden" value="{$arr.id}" name="id" id="id" data-value="{$arr.id}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户名:</label>
                            <div class="col-sm-8">
                                {$arr.name}
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户组:</label>
                            <div class="col-sm-8">
                                <select class="form-control m-b" name="groupid" id="groupid" data-value="{$arr.group_id}">
                                    <volist name="groupInfo" id="v">
                                        <option value="{$v.id}" <eq name="v.id" value="$arr.group_id"> selected</eq> />{$v.title}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">真实姓名:</label>
                            <div class="col-sm-8">
                                <input id="realName" name="realName" class="form-control" value="{$arr.realname}" data-value="{$arr.realname}" type="text" aria-required="true" aria-invalid="false" class="valid">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">上传头像:</label>
                            <div class="col-sm-8">
                                <input id="img" name="img" class="form-control" type="file" aria-required="true" aria-invalid="false" class="valid">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">状态:</label>
                            <div class="col-sm-8">
                                <input type="checkbox" class="js-switch form-control" value="1" data-value="{$arr.status}" name="status" id="status" <if condition="$arr.status eq 1"> checked</if>/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button class="btn btn-primary" type="submit">修改</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="__ADMIN__js/jquery.min.js"></script>
<script src="__ADMIN__js/jquery.form.js"></script>
<script src="__ADMIN__js/bootstrap.min.js"></script>
<script src="__ADMIN__js/plugins/layer.3/layer.js"></script>
<script src="__ADMIN__js/common.js"></script>
<script src="__ADMIN__js/content.min.js"></script>
<script src="__ADMIN__js/plugins/validate/jquery.validate.min.js"></script>
<script src="__ADMIN__js/plugins/validate/messages_zh.min.js"></script>
<script src="__ADMIN__js/plugins/switchery/switchery.js"></script>
<script>
    var elem = document.querySelector('.js-switch');
    var init = new Switchery(elem);

    $(function(){
        $('#editForm').ajaxForm({
            beforeSerialize:checkForm,
            success:successJump,
            type:'POST',
            dataType:'JSON'
        });
        function checkForm(){
            var submit = false;
            var objReal = $('#realName'), oldReal = objReal.data('value'), newReal = objReal.val();
            if(oldReal != newReal){
                if(oldReal == ''){
                    common.error('真实姓名不能为空');
                    return false;
                }
                submit = true;
            }else{
                objReal.removeAttr('name');
            }
            var objGid = $('#groupid'),oldGid = objGid.data('value'), newGid = objGid.val();
            if(oldGid != newGid){
                submit = true;
            }else{
                objGid.removeAttr('name');
            }
            var newImg = $('#img').val();
            if(newImg != ''){
                submit = true;
            }
//            var objStatus = $('#status'), oldStatus = objStatus.data('value'), newStatus = objStatus.val();
//            alert(oldStatus);alert(newStatus);
//            if(oldStatus != newStatus){
//                submit = true;
//            }else{
//                alert("====");
//                objStatus.removeAttr('name');
//            }

            if(!submit){
                common.success('别闹...你没有修改任何内容哦',"{:U('Admin/admin/index')}");
                return false;
            }
        }
        function successJump(data){
            if(data.status > 0){
                common.error(data.msg);
            }else{
                common.success(data.msg,"{:U('Admin/admin/index')}");
            }
        }


//        // 姓名格式验证
//        jQuery.validator.addMethod("checkname", function(value, element) {
//            if(/^[a-zA-Z0-9_-]{3,16}$/.test(value)) {
//                return true;
//            }else{
//                return false;
//            }
//        }, "");
//        $.validator.setDefaults({
//            highlight: function (e) {
//                $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
//            }, success: function (e) {
//                e.closest(".form-group").removeClass("has-error").addClass("has-success")
//            }, errorElement: "span", errorPlacement: function (e, r) {
//                e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
//            }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
//        }), $().ready(function () {
//            var e = "<i class='fa fa-times-circle'></i> ";
//
//            $("#editForm").validate({
//                rules: {
//                    name: {required: true, minlength: 5, maxlength: 15, checkname: true},
//                    realName: "required",
//                    groupid: "required"
//                },
//                messages: {
//                    name: {required: e + "请输入您的用户名", minlength: e + "用户名必须5个字符以上", maxlength: e + "用户名必须小于15个字符", checkname: e + "用户名必须为5-15位数字或字母"},
//                    realName: e + "请输入您的真实姓名",
//                    groupid: e + "请为管理员选择所属用户组"
//                },
//                submitHandler: function(form){
//                    var objectArr = $(form).serializeArray();
//                    var data = new Object();
//                    $.each(objectArr,function(k,v){
//                        data[v.name] = v.value;
//                    });
//
//                    $.post(
//                        "{:U('Admin/admin/Save')}",
//                        data,
//                        function(data){
//                            if(data.status > 0){
//                                common.error(data.msg);
//                            }else{
//                                common.success(data.msg,"{:U('Admin/admin/index')}");
//                            }
//                        },
//                        'JSON'
//                    );
//                }
//            });
//        });
    });
</script>
</body>
</html>
